﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AllenData
{
    public class TopicData
    {
        public Topic GetTopic(int topicId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                var topic = ctx.Topics.Where(d => d.TopicID == topicId);
                return topic.FirstOrDefault();
            }
        }

        public List<Topic> GetAllTopics()
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                return ctx.Topics.ToList();
            }
        }

        public int AddNewTopic(Topic topic)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                ctx.AddToTopics(topic);
                ctx.SaveChanges();

                Topic newTopic = ctx.Topics.Where(
                    d => d.Name == topic.Name).FirstOrDefault();

                if (newTopic == null)
                    return -1;
                else
                    return newTopic.TopicID;
            }
        }

        public void UpdateTopic(Topic topic)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                Topic dbDept = ctx.Topics.First(d => d.TopicID == topic.TopicID);
                ctx.ApplyCurrentValues("Topics", topic);
                ctx.SaveChanges();
            }
        }

        public void DeleteTopic(int topicId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                Topic dbDept = ctx.Topics.First(d => d.TopicID == topicId);
                ctx.DeleteObject(dbDept);
                ctx.SaveChanges();
            }
        }
    }
}
